package com.jsmile.cloud.authc.sys;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.authc.api.req.sys.ReqSysResourcePageQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysResourceQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysResourceUpdate;
import com.jsmile.cloud.authc.api.res.ResSysResources;
import com.jsmile.cloud.authc.sys.model.SysResources;
import com.jsmile.cloud.authc.sys.repo.SysResourcesRepository;
import com.jsmile.cloud.authc.sys.repo.SysResourcesSearchRepository;
import com.jsmile.mall.api.exception.ExceptionKit;

import cn.hutool.core.bean.BeanUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@AllArgsConstructor
public class SysResourcesApplicationService {

    private final SysResourcesSearchRepository sysResourcesSearchRepository;
    private final SysResourcesRepository sysResourcesRepository;

    public IPage<ResSysResources> getPageList(ReqSysResourcePageQuery reqSysResourcePageQuery) {
        ExceptionKit.checkNotNull(reqSysResourcePageQuery.getCurTenantId(), "租户id不能为空!");
        log.info("资源查询:{]", reqSysResourcePageQuery);
        return sysResourcesSearchRepository.pageQuery(reqSysResourcePageQuery).convert(x -> BeanUtil.toBean(x, ResSysResources.class));
    }

    public List<ResSysResources> getList(ReqSysResourceQuery reqSysResourceQuery) {
        ExceptionKit.checkNotNull(reqSysResourceQuery.getCurTenantId(), "租户id不能为空!");
        return sysResourcesSearchRepository.getList(reqSysResourceQuery).stream().map(x -> BeanUtil.toBean(x, ResSysResources.class)).collect(Collectors.toList());
    }

    public boolean saveResources(ReqSysResourceUpdate reqSysResourceUpdate) {
        return sysResourcesRepository.save(SysResources.create(reqSysResourceUpdate));
    }

    public boolean updateResources(ReqSysResourceUpdate reqSysResourceUpdate) {
        return sysResourcesRepository.updateById(SysResources.create(reqSysResourceUpdate));
    }

    public boolean deleteResources(ReqSysResourceUpdate reqSysResourceUpdate) {
        return sysResourcesRepository.removeById(reqSysResourceUpdate.getId());
    }
}
